package com.adaxi.adc.business.passenger.filter;


import com.adaxi.adc.business.passenger.utils.JwtUtil;
import com.adaxi.adc.common.common.CommonResult;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author 涛哥
 * @version 1.0
 */
@Slf4j
@Component
public class GateWayWallIntercepter implements HandlerInterceptor {
    @Autowired
    JwtUtil jwtUtil;
    @Value("${my.tokenKey}")
    String tokenKey;
    @Value("${my.enableTokenValid}")
    boolean  isEnableValid;

    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        System.out.println(request.getRequestURI()+ "  passenger");

        //token验证开关
        if(!isEnableValid){
            return true;
        }
        System.out.println("tokenKey " + tokenKey);
        //从请求头中获取 token
        String token =request.getHeader(tokenKey);
        System.out.println(" 验证token " + token);
        if(token!=null){
            //验证token的合法性
            boolean  isValid =jwtUtil.validateToken(token);
            if(isValid){
                return true;
                //判断是否在redis中存在
                /**
                 *   if(enableRedisToken){
                 *                    Boolean  bool= redisServiceUtil.hexists("token_valid_list",token);
                 *                    if(bool){
                 *                        return true;
                 *                    }
                 *                 }
                 *        */

            }

        }
        response.setStatus(403);
        response.setContentType("application/json;charset=utf-8");
        response.getWriter()
                .write(JSON.toJSONString(CommonResult.FAILURE("禁止翻墙，请走网关")));
        return false;
    }
}
